<?php

namespace JLPay\Services\Trans\Common;

use JLPay\Core\OrgConfig;
use JLPay\Services\BaseService;
use JLPay\Models\Trans\Common\PreAuthQueryRequest;
use JLPay\Models\Trans\Common\PreAuthQueryResponse;

/**
 * 预授权查询服务
 */
class PreAuthQueryService extends BaseService
{
    /**
     * 构造函数
     *
     * @param OrgConfig $config 配置对象
     */
    public function __construct(OrgConfig $config)
    {
        parent::__construct($config);
    }

    /**
     * 获取API URL
     *
     * @param string $requestClass 请求类名
     * @param string|null $apiPath 接口路径
     * @return string 完整的API URL
     */
    public function getApiUrl(string $requestClass, ?string $apiPath = null): string
    {
        $baseUrl = $this->config->getUrl();
        
        if ($apiPath) {
            return $baseUrl . $apiPath;
        }
        
        // 直接使用Request类中定义的API路径
        if (method_exists($requestClass, 'getApiPath')) {
            // 创建临时实例来调用getApiPath方法
            $tempInstance = new $requestClass();
            $apiPath = $tempInstance->getApiPath();
            if ($apiPath) {
                return $baseUrl . $apiPath;
            }
        }
        
        // 如果没有定义API路径，使用默认路径
        return $baseUrl . "/open/trans";
    }

    /**
     * 预授权查询接口
     *
     * @param PreAuthQueryRequest $request 预授权查询请求
     * @return PreAuthQueryResponse 预授权查询响应
     */
    public function preAuthQuery(PreAuthQueryRequest $request): PreAuthQueryResponse
    {
        return $this->post($request, PreAuthQueryResponse::class);
    }
} 